﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using AjaxControlToolkit;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.ObjectModel;

namespace DMS.ExportCSVControls.TurnersDateFilter
{
    [ToolboxItemAttribute(false)]
    public class TurnersDateFilter : System.Web.UI.WebControls.WebParts.WebPart, ITransformableFilterValues
    {
        Label dateLabel = null;
        TextBox dateControl = null;
        CalendarExtender calendar = null;

        private DateTime _defaultDate = new DateTime(2000, 1, 1);
        private Boolean _currentDate = false;
        private string _labelValue = String.Empty;

        /// <summary>
        /// The label
        /// </summary>
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Label value")]
        public string LabelValue
        {
            get { return _labelValue; }
            set { _labelValue = value; }
        }

        /// <summary>
        /// The Default Date if not specified.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Default Date & Time")]
        public DateTime DefaultDate
        {
            get { return _defaultDate; }
            set { _defaultDate = value; }
        }

        /// <summary>
        /// Flag to default date to current date.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Default to Current Date")]
        public Boolean CurrentDate
        {
            get { return _currentDate; }
            set { _currentDate = value; }
        }

        public TurnersDateFilter()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            dateLabel = new Label();
            this.Controls.Add(dateLabel);

            dateControl = new TextBox();
            dateControl.ID = "dateControl";
            dateControl.AutoPostBack = true;
            dateControl.Text = CurrentDate ? DateTime.Now.ToShortDateString() : DefaultDate.ToShortDateString();
            dateControl.TextChanged += new EventHandler(dateControl_TextChanged);
            this.Controls.Add(dateControl);

            calendar = new CalendarExtender();
            calendar.TargetControlID = dateControl.ID;
            calendar.Format = "dd/MM/yyyy";
            this.Controls.Add(calendar);
        }

        void dateControl_TextChanged(object sender, EventArgs e)
        {
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.EnsureChildControls();
            RenderContents(writer);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            EnsureChildControls();

            // Render some style properties.
            writer.Write(@"
                <style type='text/css'>
                    .datefiltercontainer span {
                        width: 100px;
                        color:#000000; 
                        font-weight:bold;
                        font-size: 1em; }
                    .datefiltercontainer input { margin-left: 10px; }
                </style>
                <div class='datefiltercontainer' style='float: left; margin: 5px; margin-bottom: 0px;'>");

            if (!String.IsNullOrEmpty(LabelValue))
                dateLabel.Text = LabelValue;

            dateLabel.RenderControl(writer);

            // Render all remaining content.
            dateControl.RenderControl(writer);
            calendar.RenderControl(writer);
            writer.Write("</div>");
        }

        #region ITransformableFilterValues Members

        public bool AllowAllValue
        {
            get { return true; }
        }

        public bool AllowEmptyValue
        {
            get { return true; }
        }

        public bool AllowMultipleValues
        {
            get { return true; }
        }

        public string ParameterName
        {
            get { return "FilterDates"; }
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<string> ParameterValues
        {
            get
            {
                DateTime? thedate = null;
                try
                {
                    thedate = Convert.ToDateTime(dateControl.Text);
                }
                catch
                {
                    thedate = new DateTime(2000, 1, 1);
                }

                string datevalue = SPUtility.CreateISO8601DateTimeFromSystemDateTime(thedate.Value);


                string[] values = new string[] { datevalue };
                return new ReadOnlyCollection<string>(values);
            }
        }

        [ConnectionProvider("Date Filter", "DateFilterProvider", AllowsMultipleConnections = true)]
        public ITransformableFilterValues SetConnectionInterface()
        {
            return this;
        }


        #endregion
    }
}
